package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseBaseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;


@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService  courseBaseService;

    @Autowired
    private CoursePubService coursePubService;

    @GetMapping("course/preview/{courseId}/{companyId}")
    public Object previewCourse(@PathVariable Long courseId,@PathVariable Long companyId) {
        Map<String, Object> dataMap = coursePubService.previewCourse(courseId, companyId);
        // 使用视图和数据模型对象
        ModelAndView modelAndView = new ModelAndView("learing_article",dataMap);
        return modelAndView;
    }

    @PostMapping("course/list")
    public PageVO  queryCourseList(PageRequestParams params, @RequestBody QueryCourseBaseModel model) {
        // 通过工具类获得请求头中的令牌，并解析获得companyid
        Long companyId = SecurityUtil.getCompanyId();
        return courseBaseService.queryCourseList(params,model,companyId);
    }

    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO baseVO) {
        Long companyId = SecurityUtil.getCompanyId();
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(baseVO);
        dto.setCompanyId(companyId);
        // 调用service并返回结果
        CourseBaseDTO resultDTO = courseBaseService.createCourseBase(dto);
        return resultDTO;
    }

    @GetMapping("course/{courseBaseId}")
    public CourseBaseDTO getCourseBaseById(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        CourseBaseDTO dto = courseBaseService.getCourseBaseById(courseBaseId, companyId);
        return dto;
    }

    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO baseVO) {
        Long companyId = SecurityUtil.getCompanyId();
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(baseVO);
        dto.setCompanyId(companyId);
        CourseBaseDTO resultDTO = courseBaseService.modifyCourseBase(dto, companyId);
        return resultDTO;
    }

    @GetMapping("course/commit/{courseId}")
    public void commitCourseBase(@PathVariable Long courseId) {

        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.commitCourseBase(courseId,companyId);

    }
}
